#include <unity.h>
#include "auth_service.h"
#include "qpcpp.hpp"
#include "bsp.h"
#include <thread>
#include <chrono>
#include <spdlog/spdlog.h>

// Test result flags
static bool test_event_received = false;

// Event handler
static void onTestEvent(QP::QEvt const* e) {
    test_event_received = true;
    spdlog::info("Test event received");
}

// Use the global AuthActiveObject instance
extern AuthActiveObject* const the_AO_Auth;

void setUp(void)
{
    // Initialize QP framework for this test
    QP::QF::init();
    
    // Reset test result flags
    test_event_received = false;
    
    spdlog::info("Subscribing to AUTH_LOGIN_RESPONSE_SUCCESS_SIG");
    // Subscribe to test event
    the_AO_Auth->subscribe(AUTH_LOGIN_RESPONSE_SUCCESS_SIG);
    
    spdlog::info("Subscribed to test event");
}

void tearDown(void)
{
    // Clean up
}

// Test that event subscription works
void test_event_subscription(void)
{
    spdlog::info("Starting test_event_subscription");
    spdlog::info("Initial test_event_received: {}", test_event_received ? "true" : "false");
    
    // Send a test event
    QP::QEvt* testEvt = Q_NEW(QP::QEvt, AUTH_LOGIN_RESPONSE_SUCCESS_SIG);
    spdlog::info("Posting test event");
    the_AO_Auth->POST(testEvt, nullptr);
    
    // Wait a bit for the async operation to complete
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    spdlog::info("After wait - test_event_received: {}", test_event_received ? "true" : "false");
    
    // Check if test event was received
    TEST_ASSERT_TRUE(test_event_received);
}